package com.miracle.anychatdemo;

import android.content.Context;
import android.media.AudioManager;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.GregorianCalendar;

/**
 * Created by vincent_tung on 2017/12/28.
 */

public class VoicingView extends RelativeLayout{

    private View mRootView;

    private TextView mTvTime;

    private CheckBox mBtnMute;

    private CheckBox mBtnHandsFree;

    private View mBtnCut;
    Handler mHandler = new Handler();
    private int recLen = 0;

    public VoicingView(Context context) {
        super(context);
        init(context);
    }

    public VoicingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public VoicingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }


    private void init(final Context context) {
       mRootView =  LayoutInflater.from(context).inflate(R.layout.activity_voicing,this);
       mTvTime = mRootView.findViewById(R.id.mTvTime);
       mBtnCut = mRootView.findViewById(R.id.mBtnCut);
       mBtnMute = mRootView.findViewById(R.id.mBtnMute);
       mBtnHandsFree = mRootView.findViewById(R.id.mBtnHandsFree);
       mBtnHandsFree.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
           @Override
           public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
               toggleSpeaker(context);
               if (isChecked){
                   mBtnHandsFree.setBackgroundResource(R.drawable.ic_voipspeakeron);
               }else {
                   mBtnHandsFree.setBackgroundResource(R.drawable.ic_voipspeakeroff);
               }
           }
       });
    }

    /**
     *  开始计时
     */
    public void countTime() {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                recLen++;
                GregorianCalendar gc = new GregorianCalendar();
                gc.setTimeInMillis(recLen * 1000);
                java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("mm:ss");
                mTvTime.setText(format.format(gc.getTime()));
                mHandler.postDelayed(this, 1000);
            }
        }, 1000);
    }

    /**
     *  监听挂断事件
     * @param listener
     */
    public void setOnCutListener(OnClickListener listener){
        mBtnCut.setOnClickListener(listener);
    }

    /**
     * 是否静音
     * @param listener
     */
    public void setOnMuteChangeListener(CompoundButton.OnCheckedChangeListener listener){
        mBtnMute.setOnCheckedChangeListener(listener);
    }

    public CheckBox getmBtnMute() {
        return mBtnMute;
    }

    /**
     * 免提
     *
     * @param context
     */
    private void toggleSpeaker(Context context)
    {
        AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        am.setMode(AudioManager.MODE_IN_CALL);
        am.setSpeakerphoneOn(!am.isSpeakerphoneOn());
    }
}
